package com.blb.mvc.controller;

import com.blb.mvc.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author lvtian
 * @date 2022/12/29 10:57
 */
@Controller
public class UserController {

    //用于访问login页面
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    @PostMapping("/login0")
    public String login0(User user , HttpSession session , Model model){
        if("zhangsan".equals(user.getUsername()) && "123".equals(user.getPassword())){
            session.setAttribute("user", user);
            return "forward:/addStudent1";
        }
        model.addAttribute("msg","账号或密码错误");
        return "forward:/toLogin";

    }


    @RequestMapping("/login1")
    public String login1(String username, String password){
        System.out.println("username"+username);
        System.out.println("password"+password);
        return "login";
    }

    @RequestMapping("/login2")
    public String login2(@RequestParam("username")String uname, @RequestParam("password")String pwd,
                         @RequestParam(value ="age",required = false , defaultValue = "18")Integer age){
        System.out.println("username"+uname);
        System.out.println("password"+pwd);
        System.out.println("age"+age);
        return "login";
    }

    @RequestMapping("/login3")
    public String login3(User user){
        System.out.println("username"+user.getUsername());
        System.out.println("password"+user.getPassword());
        return "login";
    }

    //方法4 将参数放到url路径里面传递
    @RequestMapping("/login4/{username}/{password}")
    public String login4(@PathVariable("username") String username, @PathVariable String password){
        System.out.println("username:" + username);
        System.out.println("password:" + password);
        return "login";
    }

    @RequestMapping("/login5")
    public String login5(HttpServletRequest req , HttpServletResponse resp){
        System.out.println("username"+req.getParameter("username"));
        System.out.println("password"+req.getParameter("password"));
        return "login";
    }
}
